import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'logic.dart';

class ClearBorderEditText extends StatelessWidget {
  final logic = Get.put(EditTextLogic());
  String _editText = "";
  String _hint;
  TextInputType type; //主要控制输入值的类型，跳出键盘
  final ValueChanged<String> _onChanged; //方法，监听值
  TextEditingController controller = TextEditingController();
  FocusNode focusNode = FocusNode();
  bool isScan;
  bool isShowIcon;

  ClearBorderEditText(this._hint, this._onChanged,
      {this.type = TextInputType.text,
      FocusNode? focusNode,
      TextEditingController? controller,
      this.isScan = true,
      this.isShowIcon = true}) {
    if (focusNode != null) {
      this.focusNode = focusNode;
    }
    if (controller != null) {
      this.controller = controller;
    }
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<EditTextLogic>(
      builder: (logic) => TextField(
        controller: controller,
        focusNode: focusNode,
        keyboardType: type,
        maxLines: 3,
        decoration: InputDecoration(
            hintText: '$_hint',
            hintStyle: TextStyle(
              fontSize: 14,
            ),
            border: OutlineInputBorder(
              borderRadius: BorderRadius.all(Radius.circular(8)),
              borderSide: BorderSide(
                width: 1.0,
              ),
            ),
            //文本框的尾部图标
            suffixIcon: controller.text.length > 0 && isShowIcon
                ? IconButton(
                    //如果文本长度不为空则显示清除按钮
                    onPressed: () {
                      controller.text = "";
                      _editText = "";
                      logic.update();
                    },
                    icon: Icon(Icons.cancel, color: Colors.grey))
                : null),
        onChanged: (value) {
          if (isScan) {
            if (_editText.length == 0 && value.length > 1) {
              _onChanged(value);
            }
          } else {
            _onChanged(value);
          }
          _editText = value.toString();
          logic.update();
        },
      ),
    );
  }
}
